package com.laowang.project.system.controller;

import java.util.Date;
import java.util.List;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.MobileCheck;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.framework.security.LoginUser;
import com.laowang.framework.security.service.TokenService;
import com.laowang.project.system.domain.TbChargeSystem;
import com.laowang.project.system.domain.vo.AppUserSub;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.TbManageUser;
import com.laowang.project.system.service.TbManageUserService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

import javax.validation.constraints.NotBlank;

/**
 * 商户信息Controller
 * 
 * @author laowang
 * @date 2023-02-17
 */
@Api(tags = "商户信息接口")
@RestController
@RequestMapping("/manager")
public class TbManageUserController extends BaseController {
    @Autowired
    private TbManageUserService tbManageUserService;

    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "前端 注册",notes = "")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody AppUserSub appUserSub) {
        @NotBlank(message = "手机号不能为空") String phone = appUserSub.getPhone();
        @NotBlank(message = "密码不能为空") String password = appUserSub.getPassword();
        @NotBlank(message = "重复密码不能为空") String rePassword = appUserSub.getRePassword();
        if (!MobileCheck.isMobile(phone)){
            throw new CustomException("手机格式有误",400);
        }

        if (!password.equals(rePassword)){
            throw new CustomException("两次输入的密码不一致",400);
        }

        TbManageUser tbManageUser = tbManageUserService.selectByPhone(phone);
        if (tbManageUser != null){
            throw new CustomException("该手机号已注册");
        }

        tbManageUser =new TbManageUser();
        tbManageUser.setPhone(phone);
        tbManageUser.setPassword(password);
        tbManageUser.setCreateTime(new Date());
        tbManageUserService.insertTbManageUser(tbManageUser);

        return AjaxResult.success("注册成功");
    }

    @ApiOperation(value = "前端 登录",notes = "")
    @PostMapping("/login")
    public AjaxResult register(@Validated @RequestBody TbManageUser tbManageUser) {
        @NotBlank(message = "手机号不能为空") String phone = tbManageUser.getPhone();
        @NotBlank(message = "密码不能为空") String password = tbManageUser.getPassword();
        if (!MobileCheck.isMobile(phone)){
            throw new CustomException("手机格式有误",400);
        }

        TbManageUser tbManageUser1 = tbManageUserService.selectByPhone(phone);
        if (tbManageUser1 == null){
            throw new CustomException("该手机号未注册");
        }

        if (!tbManageUser1.getPassword().equals(password)){
            throw new CustomException("密码有误",400);
        }

        LoginUser loginUser = new LoginUser(tbManageUser1,"H5");
        String token = tokenService.createToken(loginUser);
        return AjaxResult.success("操作成功",token);
    }

    @ApiOperation(value = "前端 获取用户信息",notes = "")
    @GetMapping("/getInfoByToken")
    public AjaxResult getInfoByToken() {
        TbManageUser tbManageUser = SecurityUtils.getLoginUser().getTbManageUser();
        return  AjaxResult.success("操作成功",tbManageUser);
    }


    /**
     * 查询商户信息列表
     */
    @ApiOperation(value = "查询商户信息列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
        public TableDataInfo list(TbManageUser tbManageUser) {
        startPage();
        List<TbManageUser> list = tbManageUserService.selectTbManageUserList(tbManageUser);
        return getDataTable(list);
    }
    
    /**
     * 导出商户信息列表
     */
    @ApiOperation(value = "导出商户信息列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbManageUser tbManageUser) {
        List<TbManageUser> list = tbManageUserService.selectTbManageUserList(tbManageUser);
        ExcelUtil<TbManageUser> util = new ExcelUtil<TbManageUser>(TbManageUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取商户信息详细信息
     */
    @ApiOperation(value = "获取商户信息详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(tbManageUserService.selectTbManageUserById(id));
    }

    /**
     * 新增商户信息
     */
    @ApiOperation(value = "新增商户信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbManageUser tbManageUser) {
        return toAjax(tbManageUserService.insertTbManageUser(tbManageUser));
    }

    /**
     * 修改商户信息
     */
    @ApiOperation(value = "修改商户信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbManageUser tbManageUser) {
        return toAjax(tbManageUserService.updateTbManageUser(tbManageUser));
    }

    /**
     * 删除商户信息
     */
    @ApiOperation(value = "删除商户信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(tbManageUserService.deleteTbManageUserByIds(ids));
    }
}
